/**********************************************************************/
/*
   	Author: Michelle Han, Adapted from Nikhil Kumar
	Last Updated: June 2024
   	Description: Cleans raw March 2020 SUSENAS data. Outputs deidentified
	cleaned SUSENAS data merged with PMO data.

	This cleaning file should output 2 different datasets:
	1. Cleaned Susenas data:
	sus_mar20_deid_clean
	2. Subset of Susenas person-batch data matched with PMO:
	sus_mar20_deid_clean_merged

*/
/**********************************************************************/

*******************************************
* Setup
* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_clean_SUSENAS_mar2020.txt", text replace

* Load the data
	use "$KP_deid_susenas/Raw/Individual/SUSENAS_MAR20_deid.dta", clear
	merge m:1 renum using "$KP_deid_susenas/Raw/Household/susenas20mar_rt_diseminasi.dta", keep(1 3) nogen
	merge m:1 renum using "$KP_deid_susenas/Raw/Household/Block43-mar2020", keep(1 3) nogen

*******************************************
* Recode yes/no questions 

	la def yesno 0 "No" 1 "Yes"

	local yesno_vars r2001* ///

	foreach var of varlist `yesno_vars' {
		assert `var' == 1 | `var' == 5 | `var' == .
		recode `var' (1=1) (5=0)
		replace `var' = 0 if mi(`var') // for questions dependent on previous skip logic
		la val `var' yesno
	}

*******************************************
* Demographics

* City (kotamadya) dummy
	gen kode_kab = r102
	gen city = kode_kab >= 70

* Gender
	gen gender = r405 == 1
	la def gender 1 "Male" 0 "Female"
	la val gender gender

	gen female = r405 == 2
	la def female 1 "Female" 0 "Male"
	la val female female

* Marital status
	gen marital_status = r404

* Bucket age
	gen age = r407
	recode age (0/20 = 1) (21/40 = 2) (41/60 = 3) (61/80 = 4) (81/100 = 5), gen(age_cat)

* Year DOB
    gen year_dob_sus = r406c

* Month DOB
    gen month_dob_sus = r406b
	replace month_dob_sus = . if month_dob_sus == 98

* Day DOB	
	gen day_dob_sus = r406a
	replace day_dob_sus = . if day_dob_sus == 98

* Dummy for age <=30 
	gen young = 0 
	replace young = 1 if age <= 30
	la def young 0 "Over 30 Years Old" 1 "30 and Under"
	la val young young 
	
* Education (translated to years of schooling)
	gen educ = r615
	gen school_years = .
	replace school_years = 6 if inlist(educ, 1, 2, 3, 4) // Paket A, SDLB, SD, MI
	replace school_years = 9 if inlist(educ, 5, 6, 7, 8) // Paket B, SMP LB, SMP, MTs
	replace school_years = 12 if inlist(educ, 9, 10, 11, 12, 13, 14) // Paket C, SMLB, SMA, MA, SMK, MAK
	replace school_years = 13.5 if educ == 15 // Diploma I/II
	replace school_years = 15 if educ == 16 // Diploma III
	replace school_years = 16 if inlist(educ, 17, 18) // Diploma IV / SI
	replace school_years = 18 if inlist(educ, 19, 20) // SII/Profesi
	replace school_years = 20 if educ == 21 // SIII
	replace school_years = 3 if educ == 22

* Dummy for educated (graduating high school)
	gen educated = school_years > 12 
	la def educated 0 "Less than 12 Years Schooling" 1 "12 or More Years of Schooling"
	la val educated educated

* Still in school dummy
	gen inschool = r612 ==2 if !mi(r612)

* Number of HH members
	gen hh_size = r301

* Log per-capita household consumption
	gen log_cons = log(kapita)

	gen cons_adj = kapita
	sum cons_adj if cons_adj > 0, d
	replace cons_adj = r(p99) if cons_adj > r(p99) & !mi(cons_adj)
	replace cons_adj = r(p1) if cons_adj < r(p1) & !mi(cons_adj)
	sum cons_adj, d
	gen log_adj_cons = log(cons_adj)

* Do you (applicant) live in same kabupaten you were born in?
	gen born_kab = r603
	gen born_live_same = born_kab == kode_kab

* Relation to Head of Household
    gen relation_hh_head = r403

*******************************************
* Consumption

* only in 20 for now.
	* Consumption
	* 901-food per cap
	gen cons_food = food / r301
	gen lcon_food = log(cons_food)

	gen cons_adj_food = cons_food
	sum cons_adj_food, d
	replace cons_adj_food = r(p99) if cons_adj_food > r(p99) & !mi(cons_adj_food)
	replace cons_adj_food = r(p1) if cons_adj_food < r(p1) & !mi(cons_adj_food)
	replace cons_adj_food = cons_adj_food / 1000
	gen lcon_adj_food = log(cons_adj_food)

* 902--non-food per cap
	gen cons_nfood = nonfood / r301
	gen lcon_nfood = log(cons_nfood)

	gen cons_adj_nfood = cons_nfood
	sum cons_adj_nfood, d
	replace cons_adj_nfood = r(p99) if cons_adj_nfood > r(p99) & !mi(cons_adj_nfood)
	replace cons_adj_nfood = r(p1) if cons_adj_nfood < r(p1) & !mi(cons_adj_nfood)
	replace cons_adj_nfood = cons_adj_nfood / 1000
	gen lcon_adj_nfood = log(cons_adj_nfood)

* tot consumption
	gen tot_cons = cons_food + cons_nfood
	gen ltot_cons = log(tot_cons)

	gen tot_adj_cons = tot_cons
	sum tot_adj_cons if tot_adj_cons > 0, d
	replace tot_adj_cons = r(p99) if tot_adj_cons > r(p99) & !mi(tot_adj_cons)
	replace tot_adj_cons = r(p1) if tot_adj_cons < r(p1) & !mi(tot_adj_cons)
	replace tot_adj_cons = tot_adj_cons / 1000
	sum tot_adj_cons, d
	gen ltot_adj_cons = log(tot_adj_cons)

* Durable goods 
* 5.5 kilos or more LPG canister
	gen dur_canister = r2001a 

* fridge/freezer
	gen dur_fridge = r2001b 

*  air conditioner	
	gen dur_aircon = r2001c 

* water heater 	
	gen dur_waterheat = r2001d

* telephone 
	gen dur_phone = r2001e 

* computer
	gen dur_computer = r2001f 

* gold	
	gen dur_gold = r2001g 

* motorcycle	
	gen dur_bike = r2001h 

* boat	
	gen dur_boat = r2001i

* motorboat	
	gen dur_motorboat = r2001j

* car	 
	gen dur_car = r2001k 

* flat television	
	gen dur_tv = r2001l 

* land	
	gen dur_land = r2001m

* Check
	foreach var of varlist dur_* {
		tab `var', m
		la val `var' yesno
	}

*******************************************
* Disability

* disabilities
  tab r1002, m
  gen vision_disabled = r1002 == 1 | r1002 == 2 | r1002 == 3
  gen vision_disability = r1002

  tab r1003, m
  gen hearing_disabled = r1003 == 5 | r1003 == 6 | r1003 == 7
  gen hearing_disability = r1003

  tab r1004, m
  gen walk_disabled = r1004 == 1 | r1004 == 2 | r1004 == 3
  gen walk_disability = r1004

  tab r1005, m
  gen hand_disabled = r1005 == 5 | r1005 == 6 | r1005 == 7
  gen hand_disability = r1005

	tab r1006, m
  gen att_disabled = r1006 == 1 | r1006 == 2 | r1006 == 3
  gen att_disability = r1006

	tab r1007, m
	gen emo_disabled = r1007 == 5 | r1007 == 6 | r1007 == 7
	gen emo_disability = r1007

  tab r1008, m
  gen speech_disabled = r1008 == 1 | r1008 == 2 | r1008 == 3
  gen speech_disability = r1008

  summ *disabled

  gen any_disability = (vision_disabled + hearing_disabled + walk_disabled + hand_disabled + speech_disabled + att_disabled + emo_disabled) >= 1
  tab any_disability, m

  gen severe_disability = inlist(r1002, 1, 2) | ///
													inlist(r1003, 5, 6)  | ///
													inlist(r1004, 1, 2)  | ///
													inlist(r1005, 5, 6)  | ///
													inlist(r1006, 1, 2)  | ///
													inlist(r1007, 5, 6)  | ///
													inlist(r1008, 1, 2) 
  tab severe_disability


*******************************************
* Food Insecurity
	gen insuff_food = r1701==1
	gen nonhealth_food = r1702==1
	gen fewtype_food = r1703==1
	gen missmeal = r1704==1
	gen eatless = r1705==1
	gen outoffood = r1706==1
	gen hungry = r1707==1
	gen missday = r1708==1

	egen food_insecurity_index = rowtotal(insuff_food nonhealth_food fewtype_food missmeal eatless outoffood hungry missday)
	gen food_insecurity_index_n = food_insecurity_index/8

*******************************************
* Telecom

* Use internet
	gen use_internet = r808 ==1

* In the last 3 months, have you (applicant) accessed internet using PC/laptop/tablet
	gen use_internet_comp = r809_a == "A" | r809_b == "B" | r809_c == "C"

* In the last 3 months, have you (applicant) accessed internet using cellphone
	gen use_internet_phone = r809_d == "D"

	summ use_internet*

* why use internet
	gen internet_info = r811_a == "A" | r811_b == "B"
	gen internet_social = r811_c == "C" | r811_d == "D"
	gen internet_buy_sell = r811_e == "E" | r811_f == "F" | r811_i == "I"
	gen internet_entertain = r811_g == "G"
	gen use_internet_bank = r811_h == "H"
	//summ internet* if used_internet

	gen computer_use = (r807_x=="X")
	gen mobile_use = (r801==1)
	gen mobile_ownership = (r802==1)

*******************************************
* Social Assistance

* 617: receive PIP 
	gen receive_PIP = r617
	tab receive_PIP

* 2201: social security 
	gen assist_vet = r2201a ==1
	gen assist_pen = r2201b ==1
	gen assist_workinsur = r2201c==1
	gen assist_lifeinsur = r2201d ==1
	gen assist_sevpay = r2201e ==1

* 2202: Family Card Hejahtera (KKS)
	gen receive_KKS = (r2202 == 1 | r2202 == 2)
	la val receive_KKS yesno
	tab receive_KKS

* 2203:  PKH last year
	gen receive_pkh_year = r2203  ==1
	tab receive_pkh_year

* 2204a:  PKH Now
	gen receive_pkh_now = r2204a ==1
	tab receive_pkh_now

* 2204b:  PKH Receipt Method
	gen pkh_receipt_method = r2204b

* 2206: receive BPNT 
	gen ever_BPNT = r2206 ==1
	tab ever_BPNT

* 2207: receive BPNT by month 
	gen feb20_BPNT = r2207a2
	tab feb20_BPNT

	gen jan20_BPNT = r2207a3
	tab jan20_BPNT

	gen dec19_BPNT = r2207a4
	tab dec19_BPNT

	gen nov19_BPNT = r2207a5
	tab nov19_BPNT

* 2209: any assistance from local governments
	gen local_assist = r2209 ==1
	tab local_assist

* 2210a: routine assistance from local governments
	gen r_assist = r2210a ==1
	tab r_assist 

* 2210b: non-routine assistance from local governments
	gen nr_assist = r2210b ==1
	tab nr_assist

*******************************************
* Health 

	gen anyhealth_issue = r1102 ==1
	gen health_affectwork = r1103 ==1
	gen hospitalized = r1201 ==1

*******************************************
* Employment

* Employed in week prior to survey
	gen employed = inlist(r706, 1, 2, 3, 4, 5)

* 706--work status questions during the "final week"
	gen employment_status = r706
	gen self_employed = r706 == 1
	gen business_owner = r706 == 2 | r706 == 3
	gen self_emp_bus_owner = (r706 == 1 | r706 == 2 | r706 == 3)
	gen perm_worker = r706 == 4
	gen freelancer = r706 == 5
	gen family_unpaid = r706 == 6

	label define emp 1 "Self-employed" 2 "Business with Temporary Employees" ///
			3 "Business with Permanent Employees" ///
			4 "Permanent Worker" 5 "Freelancer" 6 "Family/Unpaid Worker", replace
	lab val employment_status emp

	tab employed employment_status, m

* 707, 708- Hours Worked
	gen hrs_worked_main = r707
	replace hrs_worked_main = 0 if employed == 0
	gen hrs_worked_all = r708
	replace hrs_worked_all = 0 if employed == 0	

* Industry
	gen ind_code = r705

* In the last week, did you work or have work or a business but temporarily not working?  (same as SAKARNAS 9A + 10A --can also stack and add a dummy)
	gen temp_not_work = r704
	recode temp_not_work (1=1) (5=0)
	replace temp_not_work = 0 if employed
	tab temp_not_work, m
	tab temp_not_work employment_status, m
	tab temp_not_work employed, m

	bys renum: egen hh_business = max(business_owner)
	bys renum: egen hh_emp_self = max(self_employed)

* Does anyone in the household own a business with employees (either formal or informal)
	gen with_emp = inlist(r706, 2, 3)
	bys renum: egen with_emp_HH_S = max(with_emp)

*******************************************
* Assets

* Motorbike
	gen motorbike = r2001h == 1

* Gold/jewelry
	gen gold_jewel = r2001g == 1

* Flat screen TV
	gen flat_TV = r2001l == 1

*House characteristics:
*# sqm of the house
	gen hh_area = r1804

	* Savings account dummy
	gen savings_acc = r701 ==1 if !mi(r701)

*******************************************
* check duplicates
  duplicates tag anon_id4, gen(anon_id4_dupe)
  tab anon_id4_dupe

* drop duplicates 
  drop if anon_id4_dupe != 0 & !missing(anon_id4)
  drop anon_id4_dupe
/*----------------------------------------------------*/
              * Export cleaned data
/*----------------------------------------------------*/

  rename fwt weight
  datasignature
    	if "`r(datasignature)'" == "1258326:673(111568):3456703941:1780166149" {
    	save "$KP_deid_susenas/Clean/sus_mar20_deid_clean.dta", replace
      	}
  	else {
    	di as err "Careful, your machine produces a different dataset"
    	stop
		}
  

/*----------------------------------------------------*/
          * Merge with admin data and export
/*----------------------------------------------------*/

	gen sus_round = 4
	keep if !mi(anon_id4)
	drop r1* r2* r3* r4* r5* r6* r7* r8* r9*

  drop date_incentive // String-numeric mismatch with admin data. Import from there.

	merge 1:m anon_id4 using "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", nogen keep(3)


	compress
	datasignature
    	if "`r(datasignature)'" == "212947:223(65515):477019190:3565499438" {
    	save "$KP_deid_susenas/Clean/sus_mar20_deid_clean_merged.dta", replace
      	}
  	else {
    	di as err "Careful, your machine produces a different dataset"
    	stop
		}
	
